package com.manager.gen.module.controller.platform;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.manager.commons.annotation.Module;
import com.manager.commons.annotation.Operation;
import com.manager.core.shiro.config.ShiroUtil;
import com.manager.gen.module.platform.core.entity.PlatformUser;
import com.manager.gen.web.controller.AdminController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import com.manager.commons.base.DataGrid;
import com.manager.core.module.log.aop.TMLog;
import com.manager.gen.module.platform.core.service.PlatformUserService;
import com.manager.core.web.json.AjaxJson;

/**
 * 控制器
 *
 * @author H
 * @version 1.0
 */
@Controller()
@Module(parent = "account-manager", title = "用户管理",sort = 1,code = "/platform/user")
public class PlatformUserController extends AdminController<PlatformUser> {
    private final String baseUrl = "platform/user";
    @Autowired
    private PlatformUserService _service;

    /**
     * 详情页面
     *
     * @return
     */
    @TMLog(type = "mvp", description = "访问系统用户信息页面")
    @RequestMapping(baseUrl+"/info")
    @Operation(title = "详情")
    public ModelAndView info() {
        request.setAttribute("roleName", ShiroUtil.getCurrentRoleName());
        return getModelAndView(baseUrl + "/user-info", _service.findAccountView());
    }

    /**
     * 新增
     *
     * @return
     */
    @TMLog(type = "mvp", description = "访问新增用户页面")
    @RequestMapping(baseUrl+"/add")
    @Operation(title = "新增")
    public ModelAndView platformUserAdd() {
        return getModelAndView(baseUrl + "/user-edit", getModel());
    }

    /**
     * 新增
     *
     * @return
     */
    @TMLog(type = "mvp", description = "访问新增用户页面")
    @RequestMapping(baseUrl+"/edit")
    @Operation(title = "编辑")
    public ModelAndView platformUserEdit() {
        return getModelAndView(baseUrl + "/user-edit", _service.findView(getParams()));
    }

    /**
     * 删除数据
     *
     * @return
     */
    @RequestMapping(value = baseUrl+"/delete", method = RequestMethod.POST)
    public @ResponseBody AjaxJson delete() {
        return _service.delete(getParams());
    }

    /**
     * 列表
     *
     * @return
     */
    @TMLog(type = "mvp", description = "访问用户列表页面")
    @RequestMapping(baseUrl+"/index")
    public ModelAndView platformUserList() {
        return getModelAndView(baseUrl + "/user-index");
    }

    /**
     * 分页
     *
     * @return
     */
    @TMLog(type = "rmp", description = "访问用户列表分页[post]。")
    @RequestMapping(value = baseUrl+"/list")
    @Operation(title = "列表")
    public @ResponseBody DataGrid platformUserListPage() {
        return _service.queryPage(getParams());
    }

    /**
     * 保存
     *
     * @returna
     */
    @TMLog(type = "rmp", description = "保存用户数据[post]。")
    @PostMapping(value = baseUrl+"/save")
    @Operation(title = "编辑保存")
    public @ResponseBody AjaxJson platformUserSave() {
        try {
            return _service.edit(getParams());
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }
        return new AjaxJson("异常错误，请稍后再试");
    }

    @TMLog(type = "mvp", description = "访问系统用户修改密码页面")
    @RequestMapping(baseUrl+"/pwd")
    public ModelAndView pwd() {
        return getModelAndView(baseUrl + "/update-password");
    }

    /**
     * 修改密码
     *
     * @return
     */
    @TMLog(type = "rmp", description = "修改个人密码[post]。")
    @PostMapping(value = baseUrl+"/pwd/save")
    public @ResponseBody AjaxJson personalPwdModify() {
        return _service.updatePwd(getParams());
    }

    /**
     * 改变数据状态
     *
     * @return
     */
    @TMLog(type = "rmp", description = "修改用户数据状态(禁用/启用)[post]。")
    @PostMapping(value = baseUrl+"/status")
    @Operation(title = "状态修改")
    public @ResponseBody AjaxJson platformUserStatus() {
        return _service.status(getParams());
    }

    /**
     * 重置密码
     *
     * @return
     */
    @TMLog(type = "rmp", description = "重置用户的密码[post]。")
    @PostMapping(value = baseUrl+"/pwd/reset")
    @Operation(title = "重置密码")
    public @ResponseBody AjaxJson platformUserPwdReset() {
        return _service.resetPwd(getParams());
    }


    @RequestMapping(value = baseUrl+"/jsUserAll")
    public void JsUserAll(HttpServletResponse response) {
        PrintWriter writer = null;
        try {
            List<PlatformUser> users = _service.queryAll();
            StringBuffer js = new StringBuffer();
            JSONObject jsonObject = new JSONObject();
            for (PlatformUser user : users) {
                jsonObject.put(user.getPuId(), user.getPuName());
            }
            js.append("var _sysUserDataObject=");
            js.append(jsonObject.toJSONString());
            js.append(";");
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/x-javascript");
            writer = response.getWriter();
            writer.print(js);
        } catch (Exception e) {
            e.printStackTrace();
            if (writer != null) {
                writer.flush();
                writer.close();
            }
        }
    }

    /**
     * json
     *
     * @return
     */
    @TMLog(type = "rmp", description = "获取用户角色json格式数据[post]。")
    @RequestMapping(value = baseUrl+"/role/jsons")
    public @ResponseBody AjaxJson platformUserRoleJsons() {
        return _service.queryAllRole();
    }

    /**
     * 保存数据
     *
     * @return
     */
    @TMLog(type = "page", description = "保存用户角色数据[post]。")
    @RequestMapping(value = baseUrl+"/role/save", method = RequestMethod.POST)
    @Operation(title = "保存用户角色")
    public @ResponseBody AjaxJson platformUserRoleSave() {
        return _service.saveRole(getParams());
    }

    @Override
    public PlatformUser getModel() {
        return new PlatformUser();
    }

}